import 'dart:convert';
import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:wanxiang_app/pages/home/searchResult.dart';
import 'package:wanxiang_app/utils/constant.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../bean/game_list_entity.dart';
import '../../utils/network_manager.dart';
import '../../utils/network_url.dart';
import '../../utils/toast_util.dart';

class SerchController extends GetxController{
  @override
  void onInit() {
    super.onInit();
    _getGameListData();
  }
  @override
  void onClose() {
    super.onClose();
  }

  late GameListData clickItem;
  changeItem(item){
    clickItem = item;
    name = clickItem.name;
    channel_name = clickItem.code;
    channel_id = clickItem.id;
    update();
  }

  String keyword = '';
  String channel_name = '';
  String channel_id = '';
  String name = '';
  List<GameListData> list = [];
  void _getGameListData(){
    DioManager.getInstance().post(ServiceUrl.gameList, null, (data) async {
      GameListEntity bannerEntity = GameListEntity.fromJson(jsonDecode(data));
      list = bannerEntity.data;
      changeItem(list[0]);
      update();
    }, (error) {
      ToastUtil.show(error);
    });
  }
}

class SearchPage extends GetView<SerchController>  {
  late SerchController controller ;
  late BuildContext context ;
  @override
  Widget build(BuildContext context1) {
    Get.lazyPut<SerchController>(() => SerchController());
    controller = Get.find();
    context = context1;

    return GetBuilder<SerchController>(
      builder: (_){
        return  Scaffold(
            backgroundColor: Colors.white,
            body:_content(
            ) );
      },
    );
  }

  Widget _content() {
    return Container(
      margin: EdgeInsets.all(20.w),
      child: Column(
        children: [
          SizedBox(
            height: MediaQueryData.fromWindow(window).padding.top ,
          ),
          _top(),
          _line(),
          // _title(),
          Expanded(child:     SingleChildScrollView(
              child:  _buildFilterChip()
          ))
        ],
      ),
    );
  }

  Widget _top() {
    return Row(
      children: [
        InkWell(
          onTap: (){
            Get.back();
          },
          child:  Image.asset(
            '${Constant.IMG_BASE}ic_back.png',
            width: 15.w,
            height: 15.h,
          )
        )
       ,
        SizedBox(
          width: 20.w,
        ),
        Expanded(
            flex: 1,
            child: Container(
              height: 32.h,
              decoration: BoxDecoration(
                  color: const Color(0xFFF5F5F5),
                  borderRadius: BorderRadius.circular(17.r)),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  SizedBox(
                    width: 18.w,
                  ),
                  Image.asset(
                    '${Constant.IMG_BASE}home_search.png',
                    width: 14.w,
                    height: 14.h,
                  ),
                  SizedBox(
                    width: 8.w,
                  ),
                  Container(
                    width: MediaQuery.of(context).size.width - 200.w,
                    alignment: Alignment.center,
                    child: TextField(

                        decoration: InputDecoration(
                          counterText: "",
                          hintText: '请输入标题或商品ID',
                          labelStyle: TextStyle(color: Color(0xFFB3B3C6), fontSize: 14.sp),
                          // contentPadding: EdgeInsets.only(left: 0, top: 14, bottom: 16),
                          hintStyle: TextStyle(color: Color(0xFFB3B3C6), fontSize: 14.sp),
                          border: const OutlineInputBorder(borderSide: BorderSide.none),
                          contentPadding: EdgeInsets.symmetric(vertical: 0),
                        ),
                      onChanged: (str) {
                        controller.keyword = str;
                        print('onChanged==='+str);
                      },
                    )
                  )

                ],
              ),
            )),
        InkWell(
          onTap: (){
            // if(controller.keyword.isEmpty){
            //   ToastUtil.show("请输入标题或商品ID");
            //   return;
            // }
            FocusScope.of(context).unfocus();
            Get.to(()=>SearchResultPage(),arguments: {
              'showFilter':true,
              'showSearch':true,
              'keyword':controller.keyword,
              'channel_name':controller.channel_name,
              'channel_id':controller.channel_id,
              'name':controller.name
            },preventDuplicates:false);
          },
          child: Container(
            margin: EdgeInsets.only(left: 16.w, right: 21.w),
            child: Text(
              '搜索',
              style: TextStyle(color: const Color(0xFF333333), fontSize: 13.sp),
            ),
          )
        )

      ],
    );
  }

  Widget _line(){
    return Container(
      margin: EdgeInsets.only(top: 17.h),
      height: 1.h,
      decoration: const BoxDecoration(
        color: Color(0xFFF5F5F5),
      ),
    );
  }

  Widget _item(item){
    return InkWell(
      onTap: (){
        controller.changeItem(item) ;
      },
      child:  RawChip(
        label: Text(item.name,style: TextStyle(color: item.id==controller.clickItem.id? const Color(0xFFFFFFFF):const Color(0xFF333333),fontSize: 13.sp),),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6.r)),
        backgroundColor:item.id==controller.clickItem.id? const Color(0xFF007BE5):const Color(0xFFF5F5F5),
        padding: const EdgeInsets.symmetric(vertical: 10),
      )
    );
  }

  _buildFilterChip(){
    return  Container(
        child:  Column(
          children: [
            SizedBox(height: 14.h,),
            Wrap(
                spacing: 15,
                children: controller.list.map((item) => _item(item)).toList()
            ),
          ],
        )
      );
  }

// Widget _title(){
//   return Container(
//     margin: EdgeInsets.only(left: 16.w,right: 30.w,top: 19.h),
//     child: Row(
//       mainAxisAlignment: MainAxisAlignment.spaceBetween,
//       children: [
//         Text('搜索历史',style: TextStyle(color: Color(0xFF333333),fontSize: 15.sp,fontWeight: FontWeight.bold),),
//         Image.asset(Constant.IMG_BASE+'icon_delete.png',width: 15.w,height: 15.w,)
//       ],
//     ),
//   );
// }
}
